package com.meichis.yndms.service;

import android.app.*;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.meichis.mcsappframework.utils.UniversalInterface;
import com.meichis.yndms.R;
import com.meichis.yndms.ui.activity.UploadProgressActivity;
import com.meichis.yndms.vm.UploadPresenter;

public class UpLoadServices extends Service {
    private final String ChannelId = "NotificationChannel_UPLoadServices";
    private IBinder binder = new UpLoadServices.LocalBinder();
    private NotificationManager nm;
    private UploadPresenter presenter;
    private UniversalInterface<Void, String> callBack;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }


    // 定义内容类继承Binder
    public class LocalBinder extends Binder {
        // 返回本地服务
        public UpLoadServices getService() {
            return UpLoadServices.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        presenter = new UploadPresenter(s -> {
            if (callBack != null) {
                callBack.todo(s);
            }
            switch (s) {
                case "success":
                case "failure":
                    break;
                case "上传完毕":
                case "网络连接失败":
                    UpLoadServices.this.stopSelf();
                    break;
                default:
                    showNotification("上传信息", s);
                    break;
            }
            return null;
        });
        showNotification("上传信息", "正在上传纪录");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        presenter.onStart();
        showNotification("上传信息", "开始上传");
        if (callBack != null) {
            callBack.todo("开始上传");
        }
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        hideNotification();
        super.onDestroy();
    }

    private void showNotification(String title, String context) {
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            NotificationChannel channel = new NotificationChannel(
                    ChannelId,
                    title,
                    NotificationManager.IMPORTANCE_LOW);
            channel.enableLights(false);
            channel.enableVibration(false);
            channel.setDescription(context);
            notificationManager.createNotificationChannel(channel);
        }

        Intent intent = new Intent(this, UploadProgressActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder builder = (new NotificationCompat.Builder(this, ChannelId))
                .setContentTitle(title)
                .setContentText(context)
                .setAutoCancel(false)
                .setOngoing(true)
                .setTicker(title)
                .setContentIntent(contentIntent)
                .setSmallIcon(R.mipmap.smallapplogo);
        Notification notification = builder.build();
        startForeground(1, notification);
    }

    private void hideNotification() {
        nm.cancel(1);
    }

    public UploadPresenter getPresenter() {
        return presenter;
    }

    public void setCallBack(UniversalInterface<Void, String> callBack) {
        this.callBack = callBack;
    }

}
